/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistance;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author chatelai
 */
public class DatabaseConnector {

    private static DatabaseConnector instance;
    private final String db = "rmi";
    private final String server = "localhost";
    private final String port = "3306";
    private final String url = "jdbc:mysql://" + server + ":" + port + "/" + db;
    private Connection connection;

    public static DatabaseConnector getInstance() throws SQLException {
        if (null == instance) { // Premier appel
            instance = new DatabaseConnector();
        }
        return instance;
    }

    private DatabaseConnector() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, "root", "");

        
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseConnector.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public ResultSet execUpdate(String query) throws SQLException {
        ResultSet result = null;
        int lastinserted_id = 0;
        Statement stmt;
       
            stmt = connection.createStatement();
//              result = connection.createStatement().executeQuery(query);
            System.out.println(query);
            lastinserted_id = stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            result = stmt.getGeneratedKeys();


      
        return result;
    }
     public ResultSet execQuery(String query) throws SQLException {
        ResultSet result = null;
        int lastinserted_id = 0;
        Statement stmt;
        
            stmt = connection.createStatement();
            System.out.println(query);
            result = stmt.executeQuery(query);
        
        return result;
    }
}
